<template>
  <div class="dashboard">
    <el-row :gutter="10">
      <el-col :span="7">
        <cy-card title="分类商品数量">
          <pie-echart :pieData="categoryGoodsCount"></pie-echart>
        </cy-card>
      </el-col>
      <el-col :span="10">
        <cy-card title="不同城市商品销售数量">
          <map-echart :mapData="addressGoodsSale"></map-echart>
        </cy-card>
      </el-col>
      <el-col :span="7">
        <cy-card title="分类商品数量">
          <rose-echart :roseData="categoryGoodsCount"></rose-echart>
        </cy-card>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="content-row">
      <el-col :span="12">
        <cy-card title="分类商品的销量">
          <bar-echart v-bind="categoryGoodsFavor" title="点赞数量"></bar-echart>
        </cy-card>
      </el-col>
      <el-col :span="12">
        <cy-card title="collection">
          <line-echart v-bind="categoryGoodsSale" title="line"></line-echart>
        </cy-card>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'
import { useStore } from '@/store'
import cyCard from '@/base-ui/card'
import {
  PieEchart,
  BarEchart,
  RoseEchart,
  LineEchart,
  MapEchart
} from '@/ components/page-echart'
export default defineComponent({
  name: 'dashboard',
  components: {
    cyCard,
    PieEchart,
    BarEchart,
    RoseEchart,
    LineEchart,
    MapEchart
  },
  setup() {
    const store = useStore()
    store.dispatch('dashboardModule/getDashboardDataAction')
    //父组件传递给pie-chart
    const categoryGoodsCount = computed(() => {
      return store.state.dashboardModule.categoryGoodsCount.map((item: any) => {
        return { name: item.name, value: item.goodsCount }
      })
    })
    const categoryGoodsSale = computed(() => {
      const xLabels: string[] = []
      const values: any[] = []
      const categoryGoodsSales = store.state.dashboardModule.categoryGoodsSale
      console.log(categoryGoodsSales)
      for (const item of categoryGoodsSales) {
        xLabels.push(item.name)
        values.push(item.goodsCount)
      }
      return { xLabels, values }
    })
    const categoryGoodsFavor = computed(() => {
      const xLabels: string[] = []
      const values: any[] = []
      const categoryGoodsFavors = store.state.dashboardModule.categoryGoodsFavor
      for (const item of categoryGoodsFavors) {
        xLabels.push(item.name)
        values.push(item.goodsFavor)
      }
      return { xLabels, values }
    })

    const addressGoodsSale = computed(() => {
      return store.state.dashboardModule.addressGoodsSale.map((item: any) => {
        return { name: item.address, value: item.count }
      })
    })

    return {
      categoryGoodsCount,
      categoryGoodsSale,
      categoryGoodsFavor,
      addressGoodsSale
    }
  }
})
</script>

<style scoped lang="less">
.content-row {
  margin-top: 20px;
}
</style>
